<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.initChart(val)
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(data) {
      this.chart = echarts.init(this.$el, 'macarons')
      if (data) {
        let xAxisList=data.xAxisList
        let yAxisDataList=data.yAxisDataList
        this.chart.setOption({
          tooltip: {
            trigger: 'axis',
            extraCssText: 'z-index:1'
          },
          grid: {
            top: '0%',
            left: '2%',
            right: '20%',
            bottom: '0%',
            containLabel: true
          },
          xAxis: [
            {
              splitLine: {
                show: false
              },
              type: 'value',
              show: false
            }
          ],
          yAxis: [
            {
              splitLine: {
                show: false
              },
              axisLine: {
                show: false
              },
              type: 'category',
              inverse: true,
              axisTick: {
                show: false
              },
              data: xAxisList
            }
          ],
          visualMap: {
            show: false,
            orient: 'horizontal',
            left: 'center',
            min: 10,
            max: 100,
            // Map the score column to color
            dimension: 0,
            inRange: {
              color: ['#74bbea', '#37b1dc', '#4fced1']
            }
          },
          series: [
            {

              name: '数量',
              type: 'bar',
              barWidth: 15,
              label: {
                show: true,
                position: 'right',
                color: '#1890FF',
                fontSize: 12,
                formatter: ({data}) => {
                  return `${data}`
                }
              },
              itemStyle: {
                borderRadius: 10,
                borderColor: '#fff',
                borderWidth: 2
              },
              data: yAxisDataList
            }
          ]
        })
      }

    }
  }
}
</script>
